package com.hnsp.edge;

import lombok.extern.slf4j.Slf4j;

/**
 * Grpc客户端构建器.
 *
 * @author ZhangJialei
 * @date 2023/6/9 11:03
 */
@Slf4j
public class GrpcBuilder {

    static String VERSION_DEFAULT = "2.0";

    /**
     * appid
     */
    private String appId = null;
    /**
     * 设备sn, 请在web端创建设备
     */
    private String sn = null;

    /**
     * 协议版本
     */
    private String version = "2.0";

    /**
     * 边缘端ip
     */
    private String targetHost = null;

    /**
     * 边缘端端口号
     */
    private int targetPort = 0;

    /**
     * 本地appip
     */
    private String localHost = null;

    /**
     * 本地端口号
     */
    private int localPort = 0;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String v) {
        this.version = v;
    }

    public String getTargetHost() {
        return targetHost;
    }

    public void setTargetHost(String targetHost) {
        this.targetHost = targetHost;
    }

    public int getTargetPort() {
        return targetPort;
    }

    public void setTargetPort(int targetPort) {
        this.targetPort = targetPort;
    }

    public String getLocalHost() {
        return localHost;
    }

    public void setLocalHost(String localHost) {
        this.localHost = localHost;
    }

    public int getLocalPort() {
        return localPort;
    }

    public void setLocalPort(int localPort) {
        this.localPort = localPort;
    }

    /**
     * 构建 EdgeGrpc
     *
     * @return
     * @throws Exception
     */
    public EdgeGrpc builde() throws Exception {
        EdgeGrpc edge = new EdgeGrpc();

        if (appId == null) {
            throw new Exception("请设置APPID");
        }
        if (sn == null) {
            throw new Exception("请设置设备SN");
        }
        if (version == null) {
            version = VERSION_DEFAULT;
            log.warn("使用默认协议版本:{}", VERSION_DEFAULT);
        }

        if (targetHost == null) {
            throw new Exception("请设置边端IP, targetHost");
        }
        if (targetPort == 0) {
            throw new Exception("请设置边端端口, targetPort");
        }
        if (localHost == null) {
            log.warn("请设置边端端口, localHost");
            throw new Exception("请设置边端端口, localHost");
        }
        if (localPort == 0) {
            throw new Exception("请设置本地app端口, localPort");
        }
        edge.setAppId(appId);
        edge.setSn(sn);
        edge.setVersion(version);

        edge.setTargetHost(targetHost);
        edge.setTargetPort(targetPort);

        edge.setLocalHost(localHost);
        edge.setLocalPort(localPort);
        return edge;
    }

}
